<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-col :md="12" :sm="12">
          操作人：{{nickname()}}
        </a-col>
        <a-col :md="12" :sm="12">
          操作日期：{{this.modDate}}
        </a-col>
        <a-divider/>
        <a-col :md="12" :sm="12">
          <a-form-item label="采购方总部编码" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <j-dict-select-tag v-decorator="['purchaserHqCode',validatorRules.purchaserHqCode,{}]" :triggerChange="true" placeholder="请选择采购方总部编码" dictCode="purchaserHqCode"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="供应商名称" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <j-dict-select-tag @change="handleInput" v-decorator="[ 'supplierCode', validatorRules.supplierCode, {}]" :triggerChange="true" placeholder="请选择供应商名称" dictCode="supplierCode" />
            <a-input v-model="model.supplierName" type="hidden"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="生产工单编码" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'woNo', validatorRules.woNo]" placeholder="请输入生产工单编码"></a-input>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="供应商生产订单编号" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'ipoNo', validatorRules.ipoNo]" placeholder="请输入供应商生产订单编号"></a-input>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="销售订单号" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'soNo', validatorRules.soNo]" placeholder="请输入销售订单号"></a-input>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="订单类型" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'orderType', validatorRules.orderType]" placeholder="请输入订单类型"></a-input>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="品类编码" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <j-dict-select-tag v-decorator="['categoryCode',validatorRules.categoryCode,{}]" :triggerChange="true" placeholder="请选择品类编码" dictCode="categoryCode"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="种类编码" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <j-dict-select-tag v-decorator="['subclassCode',validatorRules.subclassCode,{}]" :triggerChange="true" placeholder="请选择种类编码" dictCode="subclassCode"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="厂家物料编码" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'materialsCode', validatorRules.materialsCode]" placeholder="请输入厂家物料编码"></a-input>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="厂家物料描述" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'materialsDescription', validatorRules.materialsDescription]" placeholder="请输入厂家物料描述"></a-input>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="物料批次" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'materialsBatch', validatorRules.materialsBatch]" placeholder="请输入物料批次"></a-input>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="生产数量" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'amount', validatorRules.amount]" placeholder="请输入生产数量"></a-input>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="计量单位" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'unit', validatorRules.unit]" placeholder="请输入计量单位"></a-input>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="计划开始日期" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <j-date v-decorator="[ 'planStartDate', validatorRules.planStartDate]" placeholder="请输入计划开始日期"></j-date>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="计划完成日期" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <j-date v-decorator="[ 'planFinishDate', validatorRules.planFinishDate]" placeholder="请输入计划完成日期"></j-date>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="实际开始日期" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <j-date v-decorator="[ 'actualStartDate', validatorRules.actualStartDate]" placeholder="请输入实际开始日期"></j-date>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="实际完成日期" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <j-date v-decorator="[ 'actualFinishDate', validatorRules.actualFinishDate]" placeholder="请输入实际完成日期"></j-date>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="工单状态" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <j-dict-select-tag v-decorator="['woStatus',validatorRules.woStatus,{}]" :triggerChange="true" placeholder="请选择工单状态" dictCode="woStatus"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="实物ID" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'entityCode', validatorRules.entityCode]" placeholder="请输入实物ID"></a-input>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="工艺路线编码" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'processRouteNo', validatorRules.processRouteNo]" placeholder="请输入工艺路线编码"></a-input>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="工艺路线版本" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'processRevision', validatorRules.processRevision]" placeholder="请输入工艺路线版本" ></a-input>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="数据来源" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <j-dict-select-tag v-decorator="['dataSource',validatorRules.dataSource]" :triggerChange="true" placeholder="请输入数据来源" dictCode="dataSource" />
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="来源数据创建时间" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <j-date showTime="true" dateFormat="YYYY-MM-DD HH:mm:ss" v-decorator="[ 'dataSourceCreateTime', validatorRules.dataSourceCreateTime]" placeholder="请选择来源数据创建时间"></j-date>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="数据拥有方" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'ownerId', validatorRules.ownerId]" placeholder="请输入数据拥有方"></a-input>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="数据可见方" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'openId', validatorRules.openId]" placeholder="请输入数据可见方"></a-input>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="产品型号规格" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'modelSpec', validatorRules.modelSpec]" placeholder="请输入产品型号规格"></a-input>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="逻辑删除" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <j-dict-select-tag v-decorator="['isDeleted',validatorRules.isDeleted,{}]" :triggerChange="true" placeholder="请选择逻辑删除" dictCode="isDeleted" />
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="更新时间" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'modifyTime', validatorRules.modifyTime]" placeholder="请输入更新时间"></a-input>
          </a-form-item>
        </a-col>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import JDate from '@/components/jeecg/JDate'

  import { mapGetters } from 'vuex'
  import { formatDate } from '@/utils/util'
  import {initDictOptions, filterDictText} from '@/components/dict/JDictSelectUtil'

  export default {
    name: 'EipWorkOrderModal',
    components: {
      JDate
    },
    data() {
      return {
        form: this.$form.createForm(this),
        title: '操作',
        width: 800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },

        confirmLoading: false,
        supplierCodeDictOptions:[],
        validatorRules: {
          purchaserHqCode: { rules: [{ required: true, message: '请输入采购方总部编码!' }] },
          supplierCode: { rules: [{ required: true, message: '请输入供应商编码!' }] },
          supplierName: { rules: [{ required: true, message: '请输入供应商名称!' }] },
          woNo: { rules: [{ required: true, message: '请输入生产工单编码!' }] },
          ipoNo: { rules: [{ required: true, message: '请输入供应商生产订单编号!' }] },
          soNo: {},
          orderType: {},
          categoryCode: { rules: [{ required: true, message: '请输入品类编码!' }] },
          subclassCode: { rules: [{ required: true, message: '请输入种类编码!' }] },
          materialsCode: { rules: [{ required: true, message: '请输入厂家物料编码!' }] },
          materialsDescription: {},
          materialsBatch: {},
          amount: { rules: [{ required: true, message: '请输入生产数量!' }] },
          unit: { rules: [{ required: true, message: '请输入计量单位!' }] },
          planStartDate: { rules: [{ required: true, message: '请输入计划开始日期!' }] },
          planFinishDate: { rules: [{ required: true, message: '请输入计划完成日期!' }] },
          actualStartDate: {},
          actualFinishDate: {},
          woStatus: { rules: [{ required: true, message: '请输入工单状态!' }] },
          entityCode: {},
          processRouteNo: {},
          processRevision: {},
          dataSource: { rules: [{ required: true, message: '请输入数据来源!' }] },
          dataSourceCreateTime: { rules: [{ required: true, message: '请输入来源数据创建时间!' }] },
          ownerId: {},
          openId: {},
          isDeleted: { rules: [{ required: true, message: '请输入逻辑删除!' }] },
          modifyTime: {},
          modelSpec: {},
          ioflag: {},
          message: {},
          status: {},
          transno: {},
          remark1: {},
          remark2: {},
          remark3: {},
          remark4: {},
          remark5: {}
        },
        url: {
          add: '/eipWorkOrder/add',
          edit: '/eipWorkOrder/edit'
        }

      }
    },
    created() {
      this.initDictConfig()
    },
    methods: {
      ...mapGetters(['nickname', 'avatar', 'userInfo']),
      handleInput(e){
        this.model.supplierName = filterDictText(this.supplierCodeDictOptions, e);
      },
      initDictConfig() {
        this.modDate = formatDate(new Date().getTime(), 'yyyy-MM-dd hh:mm:ss')
      },
      add() {
        this.edit({})
      },
      edit(record) {
        this.form.resetFields()
        this.model = Object.assign({}, record)
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'puid', 'purchaserHqCode', 'supplierCode', 'supplierName', 'woNo', 'ipoNo', 'categoryCode', 'subclassCode', 'materialsCode', 'materialsDescription', 'materialsBatch', 'amount', 'unit', 'planStartDate', 'planFinishDate', 'actualStartDate', 'actualFinishDate', 'woStatus', 'entityCode', 'processRouteNo', 'processRevision', 'dataSource', 'dataSourceCreateTime', 'ownerId', 'openId', 'isDeleted', 'ModifyTime', 'modelSpec', 'ioflag', 'message', 'status', 'transno', 'remark1', 'remark2', 'remark3', 'remark4', 'remark5', 'soNo', 'orderType'))
        })
      },
      close() {
        this.$emit('close')
        this.visible = false
      },
      handleOk() {
        const that = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!this.model.puid) {
              httpurl += this.url.add
              method = 'post'
            } else {
              httpurl += this.url.edit
              method = 'put'
            }
            let formData = Object.assign(this.model, values)
            console.log('表单提交数据', formData)
            httpAction(httpurl, formData, method).then((res) => {
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false
              that.close()
            })
          }

        })
      },
      handleCancel() {
        this.close()
      },
      popupCallback(row) {
        this.form.setFieldsValue(pick(row, 'puid', 'purchaserHqCode', 'supplierCode', 'supplierName', 'woNo', 'ipoNo', 'categoryCode', 'subclassCode', 'materialsCode', 'materialsDescription', 'materialsBatch', 'amount', 'unit', 'planStartDate', 'planFinishDate', 'actualStartDate', 'actualFinishDate', 'woStatus', 'entityCode', 'processRouteNo', 'processRevision', 'dataSource', 'dataSourceCreateTime', 'ownerId', 'openId', 'isDeleted', 'ModifyTime', 'modelSpec', 'ioflag', 'message', 'status', 'transno', 'remark1', 'remark2', 'remark3', 'remark4', 'remark5', 'soNo', 'orderType'))
      }
    },
    mounted() {
      initDictOptions('supplierCode').then((res) => {
        if (res.success) {
          this.supplierCodeDictOptions = res.result
        }
      });
    }
  }
</script>